-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====

-- [ Command Handler ] --
function storyboard_slash(msg)
   local args = {n=0}
   local function helper(word) table.insert(args, word) end
   string.gsub(msg, "[,_\.%w]+", helper);
   

   if args[1] == "cheat" then
      if ( args[2] == "object" ) then
     if ( args[3] == "all" ) then
        storyboard_giveAllObjects();
     else
        storyboard_giveOrLooseAnObjects(args[3]);
     end
      else
     storyboard_changeCheatMode(args[2]);
      end
   elseif args[1] == "reset" then
      storyboard_resetScenar();
   elseif args[1] == "info" then
      storyboard_changeInfoConfig(args[2]);
   elseif args[1] == "posicon" then
      storyboard_changePositionIcon(args[2]);
   elseif args[1] == "coord" then
      if args[3] == nil then
          storyboard_location_helpCoord(args[2]);
      else
          storyboard_location_helpCoord(args[2] .. " " .. args[3]);
      end
   else
      storyboard_info();
   end

end

SLASH_SBOARD1 = "/sboard";
SlashCmdList["SBOARD"] = storyboard_slash;

-- [ change the cheat mode ] --
function storyboard_changeCheatMode(password)
   if ( STORYBOARD_CHEAT_MODE == false ) then
    if ( password ==  STORYBOARD_SCENARIO.PASSWORD ) then
       STORYBOARD_CHEAT_MODE = true
       local mes = STORYBOARD_MES_CONSOLEINFO_013;
       DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
    else
       local mes = STORYBOARD_MES_CONSOLEINFO_003;
       DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
    end
   else
     STORYBOARD_CHEAT_MODE = false
     local mes = STORYBOARD_MES_CONSOLEINFO_014;
    DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
   end
end

-- [ give all object ] --
function storyboard_giveAllObjects()
   if ( STORYBOARD_CHEAT_MODE ) then
      for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
    local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
      STORYBOARD_SAVED_OBJECT[id] = true;
      end
      storyboard_function_resetBag()
      local mes = STORYBOARD_MES_CONSOLEINFO_002;
      DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
   else
      local mes = STORYBOARD_MES_CONSOLEINFO_003;
      DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
   end   
end

-- [ give or loose an object ] --
function storyboard_giveOrLooseAnObjects(idObjectChosen)
   idObjectChosen = format("%d",idObjectChosen);
   idObjectChosen = idObjectChosen + 0; -- sinon ce n'est pas consid�r� comme un nombre, bizarre
   if(STORYBOARD_CHEAT_MODE) then
      for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
       local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
       if ( id == idObjectChosen) then
         STORYBOARD_SAVED_OBJECT[id] = not STORYBOARD_SAVED_OBJECT[id];
         storyboard_function_resetBag()
         local mes = STORYBOARD_MES_CONSOLEINFO_012;
         DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);         
       end
      end
   end
end
  
-- [ reset the Scenario ] --
function storyboard_resetScenar()
  storyboard_debugMessage("storyboard_resetScenar");
  local mes = STORYBOARD_MES_CONSOLEINFO_004;
  DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
  storyboard_resetVisibiltyOfObject();
  storyboard_function_resetBag()
end


-- [ active ou d�sactive l'option say ] --
function storyboard_changeInfoConfig(cmd)
   if( cmd =="say" ) then
      
      if ( STORYBOARD_SAVED.INFO_SAY_MODE ) then
     local mes = STORYBOARD_MES_CONSOLEINFO_006;
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0); 
      else
     local mes = STORYBOARD_MES_CONSOLEINFO_005;
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
      end
      STORYBOARD_SAVED.INFO_SAY_MODE = not STORYBOARD_SAVED.INFO_SAY_MODE;
      
   elseif( cmd =="answer" ) then
      
      if ( STORYBOARD_SAVED.INFO_ANSWER_MODE ) then
     local mes = STORYBOARD_MES_CONSOLEINFO_016;
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0); 
      else
     local mes = STORYBOARD_MES_CONSOLEINFO_015;
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
      end
      STORYBOARD_SAVED.INFO_ANSWER_MODE = not STORYBOARD_SAVED.INFO_ANSWER_MODE;
      
   elseif( cmd =="object" ) then
   
      if ( STORYBOARD_SAVED.INFO_OBJECT_MODE ) then
     local mes = STORYBOARD_MES_CONSOLEINFO_018;
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0); 
      else
     local mes = STORYBOARD_MES_CONSOLEINFO_017;
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
      end
      STORYBOARD_SAVED.INFO_OBJECT_MODE = not STORYBOARD_SAVED.INFO_OBJECT_MODE;
      
   else
      local mes = STORYBOARD_MES_CONSOLEINFO_019;
      DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
   end
end


--[ change la position de l'icone entre 0 et 360 degre ]--
function storyboard_changePositionIcon(cmd)
   local mes = STORYBOARD_MES_CONSOLEINFO_007;
   DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
   STORYBOARD_SAVED.POSITION = cmd;
   SbQuestIcon:SetPoint("TOPLEFT", "Minimap", "TOPLEFT", 52 - (80 * cos(STORYBOARD_SAVED.POSITION)), (80 * sin(STORYBOARD_SAVED.POSITION)) - 52);
end

-- [ Help te player to donne info localisation  ] --
function storyboard_location_helpCoord(argument)
  
  if(argument=="on") then
     local mes = STORYBOARD_MES_LOCATION_008
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
     STORYBOARD_REF_HAVELOCATIONHELP = true
  
  elseif(argument=="off") then
     local mes = STORYBOARD_MES_LOCATION_009
     DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
     STORYBOARD_REF_HAVELOCATIONHELP = false
  
  elseif(argument=="save") then
      local mes = STORYBOARD_MES_LOCATION_010
      DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
      
      SetMapZoom(GetCurrentMapContinent(),nil);
      SetMapZoom(GetCurrentMapContinent(),nil);
      local continentNames = { GetMapContinents() };
      local continent = continentNames[GetCurrentMapContinent()];
      local px, py = GetPlayerMapPosition("player");
      
      local newpx = px*10000;
      local newpy = py*10000;
      newpx = format("%d", newpx);
      newpy = format("%d", newpy);
      
      newpx = newpx / 100;
      newpy = newpy / 100;
      
      STORYBOARD_REF_POSX = newpx
      STORYBOARD_REF_POSY = newpy
      STORYBOARD_REF_HAVELOCATIONHELP = true
  else
   local mes = STORYBOARD_MES_LOCATION_011
   DEFAULT_CHAT_FRAME:AddMessage(mes,1,1,0);
   local cmpt = 0;
   argument = string.gsub(argument, ",", ".")
   for number in string.gfind(argument, "[%d\.]+") do
      if(cmpt==0) then
     STORYBOARD_REF_POSX = number;
     cmpt = cmpt + 1;
      else
     STORYBOARD_REF_POSY = number;
      end
   end
   STORYBOARD_REF_HAVELOCATIONHELP = true
  end
  
end

SLASH_SBCOORD1 = "/sbcoord";
SlashCmdList["SBCOORD"] = storyboard_location_helpCoord;
